package com.sale.succ.datacomp.client;

import static com.sale.succ.utils.TraderHelper.getLabel;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import com.sale.succ.components.MessDialog;
import com.sale.succ.objects.ClientObj;
import com.sale.succ.objects.Session;
import com.sale.succ.system.txt.ObjectsProvider;
import com.sale.succ.utils.SystemConstants;
import com.sale.succ.utils.TraderConstants;

import eve.fx.Color;
import eve.ui.Gui;
import eve.ui.table.TableCellAttributes;
import eve.ui.table.TableModel;

public class ClientModel extends TableModel {

	private String HEADERS[] = { getLabel(TraderConstants.CLIENT_NAME_COL),
			getLabel(TraderConstants.CLIENT_DOLG_COL) };
	private MessDialog loadingDialog;
	private final int NAME_COL_WIDTH = (int) (Gui.getScreenSize().width * 0.75);
	private final int NAME = 0;

	private HashMap clients;
	private HashMap row2Id;

	public ClientModel() {

		// initialization data
		loadingDialog = new MessDialog(getLabel(TraderConstants.LOADING),
				MessDialog.NOB);
		loadingDialog.exec();
		try {
			initData();
		} finally {
			loadingDialog.close(0);
		}

		numRows = clients.size();
		numCols = HEADERS.length;
		hasRowHeaders = false;
		hasColumnHeaders = true;
		fillToEqualWidths = true;
		canMoveColumnHeaders = false;
		canHScroll = true;

	}

	private void initData() {
		row2Id = new HashMap();

		clients = ObjectsProvider.getInstance().getClientCollection(
				Session.getInstance().getGroupId());

		Set cln = clients.keySet();
		Iterator it = cln.iterator();
		int i = 0;
		while (it.hasNext()) {
			row2Id.put(String.valueOf(i), it.next().toString());
			++i;
		}
	}

	/*
	 * public int search(String find) { int result = -1; find =
	 * find.toLowerCase().trim(); for (int i = 0; i < data.length; ++i) { String
	 * name = data[i];
	 * 
	 * if (name.toLowerCase().startsWith(find)) { result = i; break; } }
	 * 
	 * return result; }
	 */

	public int itemCount() {
		return clients.size();
	}

	public int calculateColWidth(int col) {
		if (col == -1)
			return 0;
		if (col != NAME) {
			return Gui.getScreenSize().width - NAME_COL_WIDTH
					- SystemConstants.SCROLL_BUTTON_W;
		}
		return NAME_COL_WIDTH;
	}

	public int calculateRowHeight(int row) {
		// if (row == -1)
		// return 0;
		return 20;
	}

	public String getSelectedClientId(int row) {
		return (String) row2Id.get(String.valueOf(row));
	}

	public Object getCellData(int row, int col) {
		if (row == -1) {
			return HEADERS[col];
		}
		ClientObj clientobj = (ClientObj) clients.get(row2Id.get(String
				.valueOf(row)));
		switch (col) {

		case 0:
			return clientobj.getClientName();
		case 1:
			return clientobj.getDebt();

		default:
			return "";
		}

	}

	public TableCellAttributes getCellAttributes(int row, int col,
			boolean isSelected, TableCellAttributes ta) {
		ta = super.getCellAttributes(row, col, isSelected, ta);
		if (row != -1) {
			ta.clipCellData = true;
			ta.anchor = WEST;
		}

		if (isSelected) {
			ta.foreground = Color.Black;
			ta.fillColor = TraderConstants.TBLMODEL_SELCOL;
		} else {
			if (row != -1) {
				String clientId = (String) row2Id.get(String.valueOf(row));

				if (Session.getInstance().containsClientId(clientId)
						&& row != -1) {
					ta.fillColor = Color.LightBlue;
				}

			}
			ta.foreground = Color.DarkGray;
			// ta.borderStyle = TableCellAttributes.BDR_NOBORDER;
		}
		return ta;
	}
}
